<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="addVisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 表格区域 -->
      <el-table style="width: 100%" :data="cateList" border stripe>
        <!-- border给表格加边框 -->
        <!-- stripe设置隔行变色 -->
        <!-- 设置type="index" 序号从1开始 -->
        <el-table-column
          label="序号"
          type="index"
          width="100"
        ></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button type="primary" size="mini" @click="showEdit(scope.row)"
              >修改</el-button
            >
            <el-button @click="del(scope.row.id)" type="danger" size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类的添加框 -->
    <el-dialog
      @closed="$refs.addForm.resetFields()"
      title="添加文字分类"
      :visible.sync="addVisible"
      width="35%"
    >
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addForm"
        label-width="70px"
        class="demo-addForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改分类的修改框 -->
    <el-dialog
      @closed="$refs.editForm.resetFields()"
      title="修改文字分类"
      :visible.sync="editVisible"
      width="35%"
    >
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="editForm"
        label-width="70px"
        class="demo-editForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="editForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="editForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="editCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      // 文章的分类列表
      cateList: [],
      addVisible: false,
      editVisible: false,
      // 添加表单的数据对象
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加表单的验证规则对象
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async initCateList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code === 0) {
        this.cateList = res.data
      }
    },
    addCate() {
      // 1.兜底校验
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) return
        // 2.发送请求
        const { data: res } = await this.$http.post(
          '/my/cate/add',
          this.addForm
        )
        // 3.根据数据提醒用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 4.关闭dialog
        this.addVisible = false
        // 5.重新获取分类列表
        this.initCateList()
      })
    },
    showEdit(row) {
      // 1.显示修改diog框
      this.editVisible = true
      console.log(row)
      // 2.将数据赋值给表框
      // 注意此时修改时由于两个地址一样，会同时修改原来的数据，因此要用深拷贝拷贝
      this.editForm = { ...row }
    },
    // 修改里面的框确定
    editCate() {
      // 1.兜底校验
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        // 2.发送请求
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // 3.根据状态提示用户
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 4.关闭修改框
        this.editVisible = false
        // 5.调用，重新刷新列表
        this.initCateList()
      })
    },
    del(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 1.发送axios
        const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
        // 2.根据状态判断
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.meessage)
        // 3.刷新页面
        this.initCateList()
        this.$message.success('删除成功')
      }).catch(() => {
      })
    }
  },
  created() {
    this.initCateList()
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
